package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.model.Category;
import com.cskaoyan.model.CategoryExample;
import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.CateGoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ：王李轶群
 * @description：商品类目模块
 * @date ：5/5/22 11:14 PM
 */
@Service
@Transactional
public class CateGoryServiceImpl implements CateGoryService {
    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 获取所有商品类目列表
     *
     * @return 商品类目列表返回对象
     */
    @Override
    public BaseRespVO<CategoryDataVO> getCategoryList() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        long total = categoryMapper.countByExample(categoryExample);
        LinkedList<CategoryVO> categoryVOS = new LinkedList<>();
        for (Category category : categories) {
            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setId(category.getId());
            categoryVO.setAddTime(category.getAddTime());
            categoryVO.setDeleted(category.getDeleted());
            categoryVO.setDesc(category.getDesc());
            categoryVO.setIconUrl(category.getIconUrl());
            categoryVO.setKeywords(category.getKeywords());
            categoryVO.setLevel(category.getLevel());
            categoryVO.setName(category.getName());
            categoryVO.setPicUrl(category.getPicUrl());
            categoryVO.setPid(category.getPid());
            categoryVO.setSortOrder(category.getSortOrder());
            categoryVO.setUpdateTime(category.getUpdateTime());
            CategoryExample categoryExample1 = new CategoryExample();
            CategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
            criteria1.andPidEqualTo(category.getId());
            criteria1.andDeletedEqualTo(false);
            List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
            categoryVO.setChildren(categories1);
            categoryVOS.add(categoryVO);
        }
        CategoryDataVO categoryDataVO = new CategoryDataVO();
        categoryDataVO.setLimit((int) total);
        categoryDataVO.setList(categoryVOS);
        categoryDataVO.setPage(1);
        categoryDataVO.setPages(1);
        categoryDataVO.setTotal((int) total);
        BaseRespVO<CategoryDataVO> categoryDataVOBaseRespVO = new BaseRespVO<>();
        categoryDataVOBaseRespVO.setErrno(0);
        categoryDataVOBaseRespVO.setErrmsg("成功");
        categoryDataVOBaseRespVO.setData(categoryDataVO);
        return categoryDataVOBaseRespVO;

    }

    /**
     * 获取类目value对应label
     *
     * @return 返回value-label
     */
    @Override
    public BaseRespVO<CategoryL1VO> getCategoryL1() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        long total = categoryMapper.countByExample(categoryExample);
        List<CategoryLabel> categoryLabels = new LinkedList<>();
        for (Category category : categories) {
            CategoryLabel categoryLabel = new CategoryLabel();
            categoryLabel.setValue(category.getId());
            categoryLabel.setLabel(category.getName());
            categoryLabels.add(categoryLabel);
        }
        CategoryL1VO categoryL1VO = new CategoryL1VO();
        categoryL1VO.setLimit((int) total);
        categoryL1VO.setList(categoryLabels);
        categoryL1VO.setPage(1);
        categoryL1VO.setPages(1);
        categoryL1VO.setTotal((int) total);
        BaseRespVO<CategoryL1VO> categoryL1VOBaseRespVO = new BaseRespVO<>();
        categoryL1VOBaseRespVO.setData(categoryL1VO);
        categoryL1VOBaseRespVO.setErrmsg("成功");
        categoryL1VOBaseRespVO.setErrno(0);
        return categoryL1VOBaseRespVO;


    }

    @Override
    public BaseRespVO<Category> createCategory(Category category) {
        category.setAddTime(new Date(System.currentTimeMillis()));
        category.setUpdateTime(new Date(System.currentTimeMillis()));
        category.setDeleted(false);
        categoryMapper.insertAndGetId(category);
        BaseRespVO<Category> categoryBaseRespVO = new BaseRespVO<>();
        categoryBaseRespVO.setData(category);
        categoryBaseRespVO.setErrmsg("成功");
        categoryBaseRespVO.setErrno(0);
        return categoryBaseRespVO;
    }

    @Override
    public BaseRespVO updateCategory(CategoryVO categoryVO) {
        String level = categoryVO.getLevel();
        if (level.equals("L2") && categoryVO.getPid() == null) {
            BaseRespVO baseRespVO = new BaseRespVO();
            baseRespVO.setErrmsg("参数不对");
            baseRespVO.setErrno(401);
            return baseRespVO;
        }
        categoryMapper.updateByPrimaryKey(categoryVO);
        BaseRespVO objectBaseRespVO = new BaseRespVO();
        objectBaseRespVO.setErrno(0);
        objectBaseRespVO.setErrmsg("成功");
        return objectBaseRespVO;
    }

    @Override
    public BaseRespVO deleteCategory(CategoryVO categoryVO) {
        List<Category> children = categoryVO.getChildren();
        if (children != null) {
            for (Category child : children) {
                child.setDeleted(true);
                categoryMapper.updateByPrimaryKeySelective(child);
            }
        }
        categoryVO.setDeleted(true);
        categoryMapper.updateByPrimaryKeySelective(categoryVO);
        BaseRespVO baseRespVO = new BaseRespVO();
        baseRespVO.setErrno(0);
        baseRespVO.setErrmsg("成功");
        return baseRespVO;
    }
}
